<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入商品名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.pro_comment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <br>
    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="commentId"/>
      <el-table-column label="商品名" align="center" prop="shopName"/>
      <el-table-column label="用户昵称" align="center" prop="nickName"/>
      <el-table-column label="记录时间" align="center" prop="recordTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回复时间" align="center" prop="replyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.replyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评论得分" align="center" prop="commentScore">
        <template slot-scope="scope">
          <el-rate v-model="scope.row.commentScore" show-text disabled/>
        </template>
      </el-table-column>
      <el-table-column label="是否匿名" align="center" prop="anonymity">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_comment_anonymity" :value="scope.row.anonymity"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_comment_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:comment:edit']"
          >修改
          </el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:comment:remove']"
          >删除
          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评论管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-row :gutter="20">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-col :span="12">
            <el-form-item label="商品名" prop="shopName">
              <el-input v-model="form.shopName" placeholder="请输入商品名" :disabled="true"/>
            </el-form-item>
            <el-form-item label="记录时间" prop="recordTime">
              <el-date-picker clearable
                              :disabled="true"
                              v-model="form.recordTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择记录时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="评论得分" prop="commentScore">
              <el-rate v-model="form.commentScore" show-text :disabled="true"></el-rate>
            </el-form-item>
            <el-form-item label="审核状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择审核状态">
                <el-option
                  v-for="dict in dict.type.pro_comment_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="订单项ID" prop="orderItemId">
              <el-input v-model="form.orderItemId" placeholder="请输入订单项ID" :disabled="true"/>
            </el-form-item>
            <el-form-item label="评论晒图" prop="pics" >
              <image-upload v-model="form.pics"  />
            </el-form-item>

          </el-col>

          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入用户昵称" :disabled="true"/>
            </el-form-item>
            <el-form-item label="回复时间" prop="replyTime">
              <el-date-picker clearable
                              v-model="form.replyTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择回复时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="是否匿名" prop="anonymity">
              <el-radio-group v-model="form.anonymity">
                <el-radio
                  :disabled="true"
                  v-for="dict in dict.type.pro_comment_anonymity"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="用户ID" prop="userId" v-if="form.anonymity == 0">
              <el-input v-model="form.userId" placeholder="请输入用户ID" :disabled="true"/>
            </el-form-item>
            <el-form-item label="商品ID" prop="prodId">
              <el-input v-model="form.prodId" placeholder="请输入商品ID" :disabled="true"/>
            </el-form-item>
            <el-form-item label="IP来源" prop="postIp">
              <el-input v-model="form.postIp" placeholder="请输入IP来源" :disabled="true"/>
            </el-form-item>
            <el-form-item label="评价" prop="evaluate">
              <el-select v-model="form.evaluate" placeholder="请选择评价" :disabled="true">
                <el-option
                  v-for="dict in dict.type.pro_comment_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="评论内容">
              <el-input type="textarea"
                        :rows="2"
                        placeholder="请评论内容" v-model="form.content" :disabled="true"/>
            </el-form-item>
            <el-form-item label="掌柜回复">
              <el-input type="textarea"
                        :rows="2"
                        placeholder="请评论内容" v-model="form.replyContent"/>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listComment, getComment, delComment, addComment, updateComment} from "@/api/product/comment";

export default {
  name: "Comment",
  dicts: ['pro_comment_evaluate', 'pro_comment_status', 'pro_comment_anonymity'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评论管理表格数据
      commentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shopName: [
          {required: true, message: "商品名不能为空", trigger: "blur"}
        ],
        nickName: [
          {required: true, message: "用户昵称不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "用户ID不能为空", trigger: "blur"}
        ],
        prodId: [
          {required: true, message: "商品ID不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询评论管理列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then(response => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        commentId: null,
        shopName: null,
        nickName: null,
        recordTime: null,
        replyTime: null,
        commentScore: 0,
        anonymity: null,
        status: null,
        userId: null,
        orderItemId: null,
        prodId: null,
        content: null,
        pics: null,
        replyContent: null,
        postIp: null,
        evaluate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const commentId = row.commentId || this.ids
      getComment(commentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改评论管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.commentId != null) {
            updateComment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commentIds = row.commentId || this.ids;
      this.$modal.confirm('是否确认删除评论管理编号为"' + commentIds + '"的数据项？').then(function () {
        return delComment(commentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/comment/export', {
        ...this.queryParams
      }, `comment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
